package com.example.demo;/**
 * Created by kete on 2018-12-03.
 */

import com.alibaba.fastjson.JSONObject;
import com.example.demo.test.*;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.apache.ibatis.session.SqlSession;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;

/**
 * 作者：kete
 * 创建时间：2018-12-03 15:48
 * 功能描述：
 * 版本：
 */
public class TaskMain {

    private ExecutorService pool;


    public void startParse() throws Exception{


        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("consumer-queue-thread-%d").build();

        pool = new ThreadPoolExecutor(8, 8, 0L, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(8),namedThreadFactory,new ThreadPoolExecutor.AbortPolicy());

//        SqlSession sqlSession = MybatisUtil.getSqlSession();
//        TempUrlMapper mapper = sqlSession.getMapper(TempUrlMapper.class);
//
//
//        List<Map<String,String>> list = mapper.queryNum();


        long start = System.currentTimeMillis();
//
//        for(int i=1;i<list.size();i++){
//            Map<String,String> temp = list.get(i);
//
//            pool.execute(new UrlParseTask(temp.get("ACCOUNTNUM")));
//
//        }

        pool.execute(new UrlParseTask("2"));

        pool.shutdown();

        while (!pool.awaitTermination(1, TimeUnit.SECONDS)) {
            System.out.println("线程还在执行。。。");
        }
        long end = System.currentTimeMillis();
        System.out.println("一共处理了【{}】---"+(end - start));

//        sqlSession.close();



    }

    public void start() throws Exception{

        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("consumer-queue-thread-%d").build();

        pool = new ThreadPoolExecutor(8, 8, 0L, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(8),namedThreadFactory,new ThreadPoolExecutor.AbortPolicy());

        SqlSession sqlSession = MybatisUtil.getSqlSession();
        TempUrlMapper mapper = sqlSession.getMapper(TempUrlMapper.class);


        List<Map<String,String>> list = mapper.queryNum();


        long start = System.currentTimeMillis();
        list.forEach(map->{
            map.entrySet().forEach(entiry->{
                pool.execute(new UrlTask(entiry.getValue()));
            });
        });

        pool.shutdown();

        while (!pool.awaitTermination(1, TimeUnit.SECONDS)) {
            System.out.println("线程还在执行。。。");
        }
        long end = System.currentTimeMillis();
        System.out.println("一共处理了【{}】---"+(end - start));

        sqlSession.close();

    }

    public void test1(String billingCycle) throws Exception{

        List<Map<String,String>> list = new ArrayList<>();

        Map<String,String> map = new HashMap<>();
        map.put("create_date","20991231");
        map.put("balanceTypeId","1");
        map.put("balance","6051");

        list.add(map);

        Map<String,String> map2 = new HashMap<>();
        map2.put("create_date","20991231");
        map2.put("balanceTypeId","4");
        map2.put("balance","0");
        list.add(map2);

        Map<String,String> map3 = new HashMap<>();
        map3.put("create_date","20191101");
        map3.put("balanceTypeId","43");
        map3.put("balance","0");
        list.add(map3);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();

        for (Map<String, String> str : list) {

            if (str.get("create_date").length() == 8) {

                Date date = new Date();
                String now = dateFormat.format(date);
                //System.out.println(now);
                Date expDate = dateFormat.parse(str.get("create_date"));// 失效时间

                Date expNow = dateFormat.parse(now);// 系统时间
                // 根据账期判断
                //判断是否是当前月份 如果不是当前月份则小于下月1日！
                String  	expDateTemp="";
                String   expNowTemp="";
                cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
                Date beginTime=cal.getTime();
                String beginTimeTemp=dateFormat.format(beginTime);
                //如果是当月获取当月1号
                expDateTemp=billingCycle.substring(0, 4)+billingCycle.substring(4,6)+"01";
                if (beginTimeTemp.equals(expDateTemp)) {
                    // 失效时间
                    expDateTemp=dateFormat.format(expDate);
                    //当前系统时间yyyymmdd
                    expNowTemp=dateFormat.format(expNow.getTime()) ;
                }else{
                    int  year= Integer.valueOf(billingCycle.substring(0, 4));
                    int   month= Integer.valueOf(billingCycle.substring(4,6));
                    cal.set(year, month, 1);
                    // 获取当前账期 保证是每月1日
                    expDateTemp=billingCycle.substring(0, 4)+billingCycle.substring(4,6)+"01";
                    //获取的下一个账期
                    expNowTemp   =   dateFormat.format(cal.getTime());
                }

                System.out.println("expDateTemp:"+expDateTemp);

                System.out.println("expNowTemp:"+expNowTemp);

            }
        }


    }

    public static void main(String[] args) {
//        try {
//            new TaskMain().startParse();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
          //测试地址
//        http://10.4.19.183:8093/i_service/bridge_zwyzs
//        String url = "http://10.4.19.167:8080/iweb/i_service/bridge_zwyzs";
//        try {
//            String temp = HttpClientUtil.me().httpPost(url,"62291375680","1810");
//            System.out.println("返回报文:"+temp);
//            JSONObject msgJson = JSONObject.parseObject(temp);
//            JSONObject respJson =  JSONObject.parseObject((CryptAesUtil.decrypt(msgJson.getString("msg"))));
//
//            System.out.println("解密返回报文:"+respJson);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        try {
            TaskMain main = new TaskMain();
            main.test1("20180701");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
